// lib/views/buy_conditions/buy_condition_list_item.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:stock_helper/data/datasource/local/database.dart';


import '../controllers/buy_conditions_controller.dart';
import 'buy_condition_form.dart';


class BuyConditionListItem extends StatelessWidget {
  final BuyCondition condition;

  const BuyConditionListItem({super.key, required this.condition});

  @override
  Widget build(BuildContext context) {
    final controller = Get.find<BuyConditionsController>();

    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      child: ListTile(
        title: Text(
          condition.conditionName,
          style: const TextStyle(fontWeight: FontWeight.bold),
        ),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (condition.listingDate != null)
              Text('上市时间: ${condition.listingDate!.toString().substring(0, 10)}'),
            if (condition.recentChange != null)
              Text('近期涨跌幅: ${condition.recentChange!.toStringAsFixed(2)}%'),
            if (condition.eps != null)
              Text('每股收益: ${condition.eps!.toStringAsFixed(2)}'),
            if (condition.navps != null)
              Text('每股净资产: ${condition.navps!.toStringAsFixed(2)}'),
            if (condition.capitalReserve != null)
              Text('资本公积金: ${condition.capitalReserve!.toStringAsFixed(2)}'),
            if (condition.attentionLevel != null)
              Text('关注级别: ${condition.attentionLevel!}'),
          ],
        ),
        trailing: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            IconButton(
              icon: const Icon(Icons.edit, color: Colors.blue),
              onPressed: () {
                controller.setEditingCondition(condition);
                Get.to(() => BuyConditionForm(condition: condition));
              },
            ),
            IconButton(
              icon: const Icon(Icons.delete, color: Colors.red),
              onPressed: () {
                Get.defaultDialog(
                  title: '确认删除',
                  content: Text('确定要删除"${condition.conditionName}"吗？'),
                  confirm: ElevatedButton(
                    onPressed: () {
                      controller.deleteBuyCondition(condition.id);
                      Get.back();
                    },
                    child: const Text('确认'),
                  ),
                  cancel: TextButton(
                    onPressed: () => Get.back(),
                    child: const Text('取消'),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}